package com.hualan.sfzy.controller.psyc;

import com.hualan.sfzy.entity.PsycTestTitle;
import com.hualan.sfzy.service.psyc.PsycTestTitleService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class PsycTestTitleController {
    @Autowired
    private PsycTestTitleService topicService;
    /**
     * 新增题目
     */
    @PostMapping("/topic")
    public Result<PsycTestTitle> add(@RequestBody PsycTestTitle topic){
        int res = topicService.add(topic);
        if(res > 0) {
            // 返回带id的topic
            return Result.success(topic);
        } else {
            return Result.error();
        }
    }
    /**
     * 删除题目
     */
    @DeleteMapping("/topic/{id}")
    public Result removeById(@PathVariable Integer id){
        return topicService.remove(id)>0?Result.success():Result.error();
    }
    /**
     * 批量删除题目
     */
    @DeleteMapping ("/topic")
    public Result remove(@RequestParam("ids") Integer[] ids){
        return topicService.removeBatch(ids)>0?Result.success():Result.error();
    }
    /**
     * 修改题目
     */
    @PutMapping("/topic")
    public Result edit(@RequestBody PsycTestTitle topic){
        return topicService.edit(topic)>0?Result.success(topic):Result.error();
    }
    /**
     * 根据id查询题目
     */
    @GetMapping("/topic/{id}")
    public Result<PsycTestTitle> findById(@PathVariable Integer id){
        PsycTestTitle topic = topicService.findById(id);
        return Result.success(topic);
    }
    /**
     * 查询所有题目
     */
    @GetMapping("/topic")
    public Result<List<PsycTestTitle>> find(){
        List<PsycTestTitle> topics=topicService.find();
        return Result.success(topics);
    }
    /**
     * 分页查询题目
     */
    @GetMapping("/topic/pages")
    public Result<Map<String,Object>> findPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize",required = false,defaultValue = "100")Integer pageSize,
                                               PsycTestTitle topic){
        Map<String,Object> map=topicService.findPage(pageNum,pageSize,topic);
        return Result.success(map);
    }
    /**
     * 根据 psykno_test 查询 topic_name 数量
     */
    @GetMapping("/topic/countByPsyknoTest/{psyknoTest}")
    public Result<Integer> countByPsyknoTest(@PathVariable Integer psyknoTest) {
        int count = topicService.countByTestId(psyknoTest);
        return Result.success(count);
    }
    /**
     * 根据 psykno_test 查询题目及其选项
     */
    @GetMapping("/topic/withChooses/{psyknoTest}")
    public Result<List<PsycTestTitle>> findWithChoosesByPsyknoTest(@PathVariable Integer psyknoTest) {
        List<PsycTestTitle> list = topicService.findWithOptionsByTestId(psyknoTest);
        return Result.success(list);
    }

}
